package com.gisocn.onemap.presenter;

import android.text.TextUtils;
import android.util.Log;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.gisocn.onemap.base.BasePresenter;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.model.QueryLocation;
import com.gisocn.onemap.utils.ExecutorsManager;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.utils.Utils;
import com.gisocn.onemap.view.QueryLocationView;
import com.google.gson.JsonObject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import jsqlite.Callback;
import jsqlite.Database;
import jsqlite.Exception;
import org.codehaus.jackson.JsonFactory;
import org.codehaus.jackson.JsonParser;

/* loaded from: classes2.dex */
public class QueryLocationPresent extends BasePresenter<QueryLocationView> {
    private String[] coldatas;
    private int index = 0;
    private Map<String, QueryLocation.QueryLayer> queryLayerMap = new HashMap();
    private Map<String, Geometry> queryLayerMaps = new HashMap();
    private ArrayList<QueryLocation.ZJDATA> zjdatas = new ArrayList<>();

    public QueryLocationPresent(QueryLocationView queryLocationView) {
        attachView(queryLocationView);
    }

    public void queryLocation(final String str, QueryLocation queryLocation) {
        this.queryLayerMap.clear();
        this.queryLayerMaps.clear();
        this.index = 0;
        if (queryLocation != null) {
            Iterator<QueryLocation.QueryLayer> it = queryLocation.getQueryLayers().iterator();
            while (it.hasNext()) {
                QueryLocation.QueryLayer next = it.next();
                String shapeFile = next.getShapeFile();
                if (!TextUtils.isEmpty(shapeFile)) {
                    String str2 = shapeFile.substring(0, shapeFile.indexOf(".")) + ".db";
                    String str3 = Constant.SAVE_PATH_SHP + str2;
                    next.setDbName(str2);
                    next.setDbPath(str3);
                    File file = new File(str3);
                    if (!this.queryLayerMap.containsKey(shapeFile) && file.exists()) {
                        this.queryLayerMap.put(shapeFile, next);
                    }
                }
            }
        }
        if (this.queryLayerMap.size() > 0) {
            ExecutorsManager.getInstance().execute(new Runnable() { // from class: com.gisocn.onemap.presenter.QueryLocationPresent.1
                @Override // java.lang.Runnable
                public void run() {
                    for (Map.Entry entry : QueryLocationPresent.this.queryLayerMap.entrySet()) {
                        QueryLocationPresent.this.queryDb((QueryLocation.QueryLayer) entry.getValue(), str);
                    }
                }
            });
        } else {
            ((QueryLocationView) this.mvpView).fail("没有相应的数据，请下载“全部属性数据”或联系管理员");
        }
    }

    void queryDb(QueryLocation.QueryLayer queryLayer, String str) {
        String substring = queryLayer.getDbName().substring(0, queryLayer.getDbName().lastIndexOf("."));
        L.e("查询的表:" + substring);
        this.coldatas = null;
        File file = new File(queryLayer.getDbPath());
        if (!file.exists()) {
            Log.e("sqlQuerySpace", "!file.exists()");
        }
        String file2 = file.toString();
        try {
            Class.forName("jsqlite.JDBCDriver").newInstance();
            Database database = new Database();
            database.open(file2, 1);
            Callback callback = new Callback() { // from class: com.gisocn.onemap.presenter.QueryLocationPresent.2
                @Override // jsqlite.Callback
                public void types(String[] strArr) {
                }

                @Override // jsqlite.Callback
                public void columns(String[] strArr) {
                    QueryLocationPresent.this.coldatas = strArr;
                }

                @Override // jsqlite.Callback
                public boolean newrow(String[] strArr) {
                    String str2;
                    if (strArr == null) {
                        return false;
                    }
                    JsonObject jsonObject = new JsonObject();
                    for (int i = 0; i < strArr.length; i++) {
                        Log.e("rowdata", QueryLocationPresent.this.coldatas[i] + ":" + strArr[i]);
                        if (!QueryLocationPresent.this.coldatas[i].equals("AsText(Geometry)")) {
                            jsonObject.addProperty(QueryLocationPresent.this.coldatas[i], strArr[i]);
                        } else if (strArr[i] != null) {
                            JsonParser jsonParser = null;
                            if (strArr[i].contains("POINT")) {
                                str2 = Utils.getPOINTWktToJson(strArr[i], Constant.WKID);
                            } else if (strArr[i].contains("LINESTRING")) {
                                if (strArr[i].contains("MULTILINESTRING")) {
                                    str2 = Utils.getMULTILINESTRINGWktToJson(strArr[i], Constant.WKID);
                                } else {
                                    str2 = Utils.getLINESTRINGWktToJson(strArr[i], Constant.WKID);
                                }
                            } else if (!strArr[i].contains("POLYGON")) {
                                str2 = null;
                            } else if (strArr[i].contains("MULTIPOLYGON")) {
                                str2 = Utils.getMULTIPOLYGONWktToJson(strArr[i], Constant.WKID);
                            } else {
                                str2 = Utils.getPOLYGONWktToJson(strArr[i], Constant.WKID);
                            }
                            try {
                                jsonParser = new JsonFactory().createJsonParser(str2);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            QueryLocationPresent.this.queryLayerMaps.put(strArr[0], GeometryEngine.jsonToGeometry(jsonParser).getGeometry());
                        }
                    }
                    return false;
                }
            };
            Iterator<QueryLocation.QueryLayer.LocationqueryField> it = queryLayer.getQueryFields().iterator();
            String str2 = "";
            String str3 = "";
            while (it.hasNext()) {
                QueryLocation.QueryLayer.LocationqueryField next = it.next();
                if (TextUtils.isEmpty(str2)) {
                    str2 = next.getFieldName() + " like '%" + str + "%'";
                } else {
                    str2 = str2 + " or " + next.getFieldName() + " like '%" + str + "%'";
                }
                if (TextUtils.isEmpty(str3)) {
                    str3 = next.getFieldName();
                } else {
                    str3 = str3 + "," + next.getFieldName();
                }
            }
            String str4 = ("SElECT " + str3 + ", AsText(Geometry) from " + substring + " where ") + str2;
            database.exec(str4, callback);
            database.close();
            int i = this.index + 1;
            this.index = i;
            if (i == this.queryLayerMap.size()) {
                L.e("查询结束" + this.queryLayerMaps.size());
                ((QueryLocationView) this.mvpView).queryResult(this.queryLayerMaps);
            }
            L.e("querySQl" + str4);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e2) {
            e2.printStackTrace();
        } catch (InstantiationException e3) {
            e3.printStackTrace();
        } catch (Exception e4) {
            e4.printStackTrace();
            L.e("---地名定位" + e4.getMessage());
            int i2 = this.index + 1;
            this.index = i2;
            if (i2 == this.queryLayerMap.size()) {
                L.e("查询结束" + this.queryLayerMaps.size());
                ((QueryLocationView) this.mvpView).queryResult(this.queryLayerMaps);
            }
        }
    }

    public void queryZJInfo(final QueryLocation queryLocation, final int i) {
        String zJData = i == 0 ? queryLocation.getZJData() : queryLocation.getCJData();
        String substring = zJData.substring(0, zJData.lastIndexOf("."));
        L.e("查询的表:" + substring);
        this.coldatas = null;
        this.zjdatas.clear();
        File file = new File(Constant.SAVE_PATH_SHP + (substring + ".db"));
        if (!file.exists()) {
            Log.e("sqlQuerySpace", "!file.exists()");
            return;
        }
        String file2 = file.toString();
        try {
            Class.forName("jsqlite.JDBCDriver").newInstance();
            Database database = new Database();
            database.open(file2, 1);
            Callback callback = new Callback() { // from class: com.gisocn.onemap.presenter.QueryLocationPresent.3
                @Override // jsqlite.Callback
                public void types(String[] strArr) {
                }

                @Override // jsqlite.Callback
                public void columns(String[] strArr) {
                    QueryLocationPresent.this.coldatas = strArr;
                }

                @Override // jsqlite.Callback
                public boolean newrow(String[] strArr) {
                    String str;
                    if (strArr == null) {
                        return false;
                    }
                    QueryLocation.ZJDATA zjdata = new QueryLocation.ZJDATA();
                    for (int i2 = 0; i2 < strArr.length; i2++) {
                        if (!QueryLocationPresent.this.coldatas[i2].equals("AsText(Geometry)")) {
                            if (QueryLocationPresent.this.coldatas[i2].equals(i == 0 ? queryLocation.getZJMCField() : queryLocation.getCJMCField())) {
                                zjdata.setMcValue(strArr[i2]);
                            }
                            if (QueryLocationPresent.this.coldatas[i2].equals(i == 0 ? queryLocation.getZJGLField() : queryLocation.getCJGLField())) {
                                zjdata.setGlFieldValue(strArr[i2]);
                            }
                        } else if (strArr[i2] != null) {
                            JsonParser jsonParser = null;
                            if (strArr[i2].contains("POINT")) {
                                str = Utils.getPOINTWktToJson(strArr[i2], Constant.WKID);
                            } else if (strArr[i2].contains("LINESTRING")) {
                                if (strArr[i2].contains("MULTILINESTRING")) {
                                    str = Utils.getMULTILINESTRINGWktToJson(strArr[i2], Constant.WKID);
                                } else {
                                    str = Utils.getLINESTRINGWktToJson(strArr[i2], Constant.WKID);
                                }
                            } else if (!strArr[i2].contains("POLYGON")) {
                                str = null;
                            } else if (strArr[i2].contains("MULTIPOLYGON")) {
                                str = Utils.getMULTIPOLYGONWktToJson(strArr[i2], Constant.WKID);
                            } else {
                                str = Utils.getPOLYGONWktToJson(strArr[i2], Constant.WKID);
                            }
                            try {
                                jsonParser = new JsonFactory().createJsonParser(str);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            zjdata.setGeometry(GeometryEngine.jsonToGeometry(jsonParser).getGeometry());
                        }
                    }
                    QueryLocationPresent.this.zjdatas.add(zjdata);
                    return false;
                }
            };
            String str = "";
            if (i == 0) {
                str = "SElECT " + queryLocation.getZJMCField() + "," + queryLocation.getZJGLField() + ", AsText(Geometry) from " + substring + " group by " + queryLocation.getZJGLField();
            } else if (i == 1) {
                str = "SElECT " + queryLocation.getCJMCField() + "," + queryLocation.getCJGLField() + ", AsText(Geometry) from " + substring + " group by " + queryLocation.getCJGLField();
            }
            L.e(str);
            database.exec(str, callback);
            database.close();
            ((QueryLocationView) this.mvpView).queryCityInfoResult(this.zjdatas, i);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e2) {
            e2.printStackTrace();
        } catch (InstantiationException e3) {
            e3.printStackTrace();
        } catch (Exception e4) {
            e4.printStackTrace();
        }
    }
}
